﻿using MediatR;
using W.Common.Enum;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using W.Warehouse.Domain.DTO;
using W.Warehouse.Read.Api.Application.Command.AllocationCommand;
using Microsoft.AspNetCore.StaticFiles;
using System.Collections.Generic;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using W.Warehouse.Domain.AllocationProduct;
using Microsoft.AspNetCore.Authorization;
using System.Security.Claims;
using System.IdentityModel.Tokens.Jwt;

namespace W.Warehouse.Read.Api.Controllers
{
    /// <summary>
    /// 调拨管理读控制器
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    [Authorize]
    public class AllocationController : ControllerBase
    {
        private readonly IMediator _mediator;
        public AllocationController(IMediator mediator)
        {
            _mediator = mediator;
        }
        /// <summary>
        /// 单据类型
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public dynamic GetAuditStatus()
        {
            return Enum.GetValues(typeof(AuditStatus))
                .Cast<AuditStatus>()
                .Select(x => new
                {
                    Value = (int)x,
                    Text = x.ToString()
                }).ToList();
        }
        /// <summary>
        /// 调拨列表显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<AllocationProductDTO>>> GetAllocationList([FromQuery]AllocationShowCommand command)
        {
            ApiResult<PageDTO<AllocationProductDTO>> result=new ApiResult<PageDTO<AllocationProductDTO>>();
            try
            {
                //var uid=HttpContext.User.FindFirst(ClaimTypes.NameIdentifier)?.Value;//获取用户id
                //var sid = HttpContext.User.FindFirst(JwtRegisteredClaimNames.Sid)?.Value;//获取公司id
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 导出调拨产品数据
        /// </summary>
        /// <param name="command"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> ExportExcel([FromQuery] AllocationShowCommand command)
        {
            var list = await _mediator.Send(command);

            IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet("调拨产品信息");

            IRow rowl = sheet.CreateRow(0);
            rowl.CreateCell(0).SetCellValue("调拨单号");
            rowl.CreateCell(1).SetCellValue("产品名称");
            rowl.CreateCell(2).SetCellValue("产品编号");
            rowl.CreateCell(3).SetCellValue("批次");
            rowl.CreateCell(4).SetCellValue("规格");
            rowl.CreateCell(5).SetCellValue("数量");
            rowl.CreateCell(6).SetCellValue("原仓库");
            rowl.CreateCell(7).SetCellValue("原库位");
            rowl.CreateCell(8).SetCellValue("调拨仓库");
            rowl.CreateCell(9).SetCellValue("状态");
            rowl.CreateCell(10).SetCellValue("审核人");
            rowl.CreateCell(11).SetCellValue("审核时间");


            int index = 1;
            foreach (var item in list.List)
            {
                rowl = sheet.CreateRow(index);
                rowl.CreateCell(0).SetCellValue(item.AllocationNo);
                rowl.CreateCell(1).SetCellValue(item.ProductName);
                rowl.CreateCell(2).SetCellValue(item.ProductNo);
                rowl.CreateCell(3).SetCellValue(item.ProductBatch);
                rowl.CreateCell(4).SetCellValue(item.ProductSpec);
                rowl.CreateCell(5).SetCellValue((double)item.Quantity);
                rowl.CreateCell(6).SetCellValue(item.WarehouseName);
                rowl.CreateCell(7).SetCellValue(item.BinLocationName);
                rowl.CreateCell(8).SetCellValue(item.AWarehouseName);
                rowl.CreateCell(9).SetCellValue(item.AllocationStatusText);
                rowl.CreateCell(10).SetCellValue(item.ExaminePeople);
                rowl.CreateCell(11).SetCellValue(item.ExamineTime.ToString());
                index++;
            }
            byte[] bytes;

            using (MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                bytes = ms.ToArray();
            }
            string mapPath = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            return File(bytes, mapPath, "调拨产品信息.xlsx");
        }
        /// <summary>
        /// 根据调拨id反填调拨信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<Allocation>> GetAllocationModelList([FromQuery] AllocationModelCommand command)
        {
            ApiResult<Allocation> result = new ApiResult<Allocation>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 根据调拨id,调拨产品id反填调拨产品信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<AllocationModelDTO>> GetAllocationProductModelList([FromQuery] AllocationProductModelCommand command)
        {
            ApiResult<AllocationModelDTO> result = new ApiResult<AllocationModelDTO>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 批量反填调拨产品信息
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public async Task<ApiResult<List<AllocationProductRuAWarehouseDTO>>> GetAllocationRangeProductModel(AllocationRangeModelCommand command)
        {
            ApiResult<List<AllocationProductRuAWarehouseDTO>> result = new ApiResult<List<AllocationProductRuAWarehouseDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 根据调拨编号查询调拨产品
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<AllocationProductRuAWarehouseDTO>>> GetAllocationByidList([FromQuery] AllocationShowbyIdCommand command)
        {
            ApiResult<List<AllocationProductRuAWarehouseDTO>> result = new ApiResult<List<AllocationProductRuAWarehouseDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
        /// <summary>
        /// 显示入库产品信息
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<PageDTO<AllocationProductRuAWarehouseDTO>>> GetProductStorageProductList([FromQuery] ProductStorageProductListCommand command)
        {
            ApiResult<PageDTO<AllocationProductRuAWarehouseDTO>> result = new ApiResult<PageDTO<AllocationProductRuAWarehouseDTO>>();
            try
            {
                result.Data = await _mediator.Send(command);
            }
            catch (Exception ex)
            {
                result.Code = -1;
                result.Message = ex.Message;
                throw;
            }
            return result;
        }
    }
}
